import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.*;


public class ClientUI extends JFrame implements Runnable, Observer{

	private JPanel paneimage = new JPanel();

	private JPanel panesouth = new JPanel();

	private JPanel panes1 = new JPanel();

	private JPanel panes2 = new JPanel();

	private JButton white = new JButton("WHITE");

	private JButton black = new JButton("BLACK");

	private JButton wait = new JButton("WAIT");

	private JLabel image = new JLabel();

	private Client c;

	private OthelloUI oUI;
	private int i =0;

	private boolean go = false;
	//private JLabel select = new JLabel("Select Color");

	public ClientUI(Client c, OthelloUI oUI){
		super("Select Color");
		this.c= c;
		this.oUI = oUI;
		c.addObserver(this);
		initcomponents();
		run();
		
	}

	private void initcomponents() {
		paneimage.add(image);
		white.setVisible(false);
		black.setVisible(false);



		panes1.add(white, BorderLayout.NORTH);
		white.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				try {
					oUI.setState("white");
					c.sendToServer(OColor.BLACK);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				oUI.setState("white");
				//setVisible(false);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		});

		panes1.add(black, BorderLayout.SOUTH);
		black.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {
				try {
					oUI.setState("black");
					c.sendToServer(OColor.WHITE);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				//setVisible(false);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		});

		panes2.add(wait);

		panesouth.add(panes1, BorderLayout.NORTH);
		panesouth.add(panes2, BorderLayout.SOUTH);

		this.add(paneimage, BorderLayout.NORTH);
		this.add(panesouth, BorderLayout.SOUTH);

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	@Override
	public void run() {
		pack();
		setVisible(true);
	}

	@Override
	public void update(Observable o, Object arg) {
		i++;
		
		if ( arg.equals(OColor.WHITE)){
			white.setEnabled(false);
			black.setEnabled(false);
		}
		else if ( arg.equals(OColor.BLACK)){
			black.setEnabled(false);
			white.setEnabled(false);
		}
		if (!(black.isEnabled()||white.isEnabled())){
			oUI.run();
			setVisible(false);
		}
		if ( i== 3){
			
			
			//System.out.println(i);
			wait.setVisible(false);
			black.setVisible(true);
			white.setVisible(true);
			
			pack();
		}

	}

}
